package org.ht.combotable;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.table.TableColumn;

import org.azj.logHelper.LOG;
import org.ht.controller.ControlCase;
import org.ht.entity.AbstractEntity;
import org.ht.entity.Case;
import org.ht.entity.Column;
import org.ht.viewer.CaseDialog;

/**
 * 表格辅助器
 * 
 * @author HT
 * 
 */
public class ComboTableHelper {

	private ComboTable table;
	private JMenuItem tableNameItem;
	private JPopupMenu popupMenu;

	private String pid;

	private ControlCase controller = new ControlCase();

	public ControlCase getController() {
		return controller;
	}

	public ComboTableHelper(ComboTable table) {
		this.table = table;

		popupMenu = new JPopupMenu();
		tableNameItem = new JMenuItem("");
		// 给表格添加右键弹出菜单
		addMouseListener();

	}

	/**
	 * 表格添加右键弹出菜单
	 */
	private void addMouseListener() {

		JMenuItem lookItem = new JMenuItem("查看");
		addLookAction(lookItem);
		JMenuItem updateItem = new JMenuItem("修改");
		addQueryAction(updateItem);
		JMenuItem deleteItem = new JMenuItem("删除");
		addDeleteAction(deleteItem);
		
		popupMenu.add(tableNameItem);
		popupMenu.addSeparator();
		popupMenu.add(lookItem);
		popupMenu.add(updateItem);
		popupMenu.add(deleteItem);

		table.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent evt) {
				if (evt.isPopupTrigger()) {
					// 取得右键点击所在行
					int row = table.rowAtPoint(evt.getPoint());
					int tableNameColumn = getPidColumnIndex();

					/**
					 * 取得表名并弹出菜单
					 */
					if (tableNameColumn != -1) {
						/**
						 * 修改菜单首条的名称
						 */
						pid = (String) table.getValueAt(row, tableNameColumn);
						tableNameItem.setText("住院号:" + pid);
					} else {
						pid = "";
						tableNameItem.setText("");
					}

					// 弹出菜单
					popupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
				}
			}
		});
	}

	private void addDeleteAction(JMenuItem menu) {
		menu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!pid.isEmpty()) {
					// 弹出确认删除面板
					int result = JOptionPane.showConfirmDialog(null,
							"确认删除病例号为:" + pid + "的病例吗?", "确认删除",
							JOptionPane.YES_NO_OPTION,
							JOptionPane.WARNING_MESSAGE);
					if (result == JOptionPane.YES_OPTION) {
						boolean success = true;
						try {
							success = controller.deleteCase(pid);
						} catch (Exception e1) {
							success = false;
							e1.printStackTrace();
						}
						// 删除成功,删除表格中对应行
						if (success) {
							deleteRowByPid(pid);
						}
					}
				}
			}
		});
	}

	private void addQueryAction(JMenuItem menu) {
		menu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!pid.isEmpty()) {
					CaseDialog caseDialog = null;
					try {
						LOG.WriteLog("修改病例");
						Case thecase = controller.queryCase(pid);
						caseDialog = new CaseDialog(thecase,CaseDialog.TYPE_UPDATE);
						caseDialog.setVisible(true);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					// 修改成功的话,删除表格中对应行
					if (caseDialog != null
							&& caseDialog.getColseResult() == JOptionPane.OK_OPTION) {
						deleteRowByPid(pid);
					}
				}
			}
		});
	}

	private void addLookAction(JMenuItem menu) {
		menu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!pid.isEmpty()) {
					CaseDialog caseDialog = null;
					try {
						LOG.WriteLog("查看病例");
						Case thecase = controller.queryCase(pid);
						caseDialog = new CaseDialog(thecase,CaseDialog.TYPE_LOOK);
						caseDialog.setVisible(true);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					// 修改成功的话,删除表格中对应行
					if (caseDialog != null
							&& caseDialog.getColseResult() == JOptionPane.OK_OPTION) {
						deleteRowByPid(pid);
					}
				}
			}
		});
	}
	
	private Vector<Column> vcolumn = new Vector<Column>();

	public Vector<Column> getVcolumn() {
		return vcolumn;
	}

	public void setVcolumn(Vector<Column> vcolumn) {
		this.vcolumn = vcolumn;
	}

	// 在表格中显示对象
	public void show(AbstractEntity entity, List<Column> key) {
		if (vcolumn.equals(key)) {
			// 不用改结构,直接加
		} else {
			// TODO 清除现有结果,修改结构,添加
		}
		ArrayList<String> list = new ArrayList<String>();
		for (Column cl : key) {
			list.add(cl.eName);
		}
		table.getModel().addRow(entity
				.getPropertyValueByVector(list));
		// TODO 更新条数
		//Global.statusBar.getLabel().setText("Hello");
	}

	
	// 清除表格
	public void clear(){
		ComboTableModel dtmodel = table.getModel();
		int rowcount=dtmodel.getRowCount();
		 for (int j = 0; j <rowcount; j++) {
		   dtmodel.removeRow(0);
		} 
	}
	
	/**
	 * 取得是住院号的那一列
	 * 
	 * @return
	 */
	private int getPidColumnIndex() {
		int tableNameColumn = -1;
		for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
			TableColumn selColumn = table.getColumnModel().getColumn(i);
			String columnHeader = (String) selColumn.getHeaderValue();
			if (columnHeader.equals("住院号")) {
				tableNameColumn = i;
				break;
			}
		}
		return tableNameColumn;
	}

	// 在表格中删除对象
	public void deleteRowByPid(String pid) {
		int y = getPidColumnIndex();
		if (y < 0)
			return;
		int row = -1;
		for (int x = 0; x < table.getRowCount(); x++) {
			if (table.getModel().getValueAt(x, y).equals(pid)) {
				row = x;
				break;
			}
		}
		if (row < 0)
			return;
		((ComboTableModel) (table.getModel())).removeRow(row);
	}
}
